using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

[ExecuteInEditMode]
public class LevelEnd : MonoBehaviour
{
	public delegate void LevelEndEventHandler(GameObject levelEnd);

	private List<LevelFinishActivator> activators = new List<LevelFinishActivator>();

	private bool isActive;

	public GameObject particles;

	public GameObject partInner;

	public GameObject partDiamond;

	public Material materialActive;

	public Material materialInactive;

	private bool updateActiveState;

	[method: MethodImpl(32)]
	public static event LevelEndEventHandler LevelEndEnterEvent;

	public static void FireLevelEndEnter(GameObject levelEnd)
	{
		if (LevelEnd.LevelEndEnterEvent != null)
		{
			LevelEnd.LevelEndEnterEvent(levelEnd);
		}
	}

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		LevelController.LevelFinishResetEvent += OnLevelFinishReset;
		LevelFinishActivator.LevelFinishActivatorOnEvent += OnLevelFinishActivatorOn;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		LevelFinishActivator.LevelFinishActivatorOnEvent -= OnLevelFinishActivatorOn;
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			LevelFinishActivator[] componentsInChildren = base.transform.parent.GetComponentsInChildren<LevelFinishActivator>();
			foreach (LevelFinishActivator item in componentsInChildren)
			{
				activators.Add(item);
			}
			updateActiveState = true;
		}
	}

	private void OnLevelFinishActivatorOn(Transform levelTransform)
	{
		updateActiveState = true;
	}

	private void OnLevelFinishReset(Transform levelTransform)
	{
		updateActiveState = true;
	}

	private void Update()
	{
		if (updateActiveState)
		{
			UpdateActiveState();
		}
		if ((bool)partDiamond)
		{
			partDiamond.transform.Rotate(Vector3.forward * Time.deltaTime * 50f);
		}
	}

	private void UpdateActiveState()
	{
		updateActiveState = false;
		isActive = true;
		if (activators.Count > 0)
		{
			foreach (LevelFinishActivator activator in activators)
			{
				if (!activator.isActivated)
				{
					isActive = false;
				}
			}
		}
		if (isActive)
		{
			if (particles != null)
			{
				ParticleSystem component = particles.GetComponent<ParticleSystem>();
				component.Play();
			}
			if (partInner != null && materialActive != null)
			{
				partInner.GetComponent<Renderer>().sharedMaterial = materialActive;
			}
			if (partDiamond != null && materialActive != null)
			{
				partDiamond.GetComponent<Renderer>().sharedMaterial = materialActive;
			}
			return;
		}
		if (particles != null)
		{
			ParticleSystem component2 = particles.GetComponent<ParticleSystem>();
			component2.Stop();
			component2.Clear();
		}
		if (partInner != null && materialInactive != null)
		{
			partInner.GetComponent<Renderer>().sharedMaterial = materialInactive;
		}
		if (partDiamond != null && materialInactive != null)
		{
			partDiamond.GetComponent<Renderer>().sharedMaterial = materialInactive;
		}
	}

	public void OnLevelEndTriggerEnter()
	{
		if (Game.gameState == GameState.Playing)
		{
			if (!isActive)
			{
				Debug.Log("LevelEnd -> isActive = " + isActive);
				return;
			}
			Audio.PlaySoundLevelFinish();
			FireLevelEndEnter(base.gameObject);
		}
	}

	public void OnLevelEndTriggerStay()
	{
		if (Game.gameState == GameState.Playing && isActive)
		{
			Audio.PlaySoundLevelFinish();
			FireLevelEndEnter(base.gameObject);
		}
	}

	public void ActivateAllActivators()
	{
		foreach (LevelFinishActivator activator in activators)
		{
			activator.ActivateInstant();
		}
		UpdateActiveState();
	}
}
